﻿using System;
using System.Windows;
using System.Windows.Controls;
using Model.Interfaces;
using View.Schools;
using View.Students;
using View.Teachers;
using Data;
using System.Collections.Generic;
using Model.ModelEventArgs;

namespace View
{
    public partial class MainWorkspace
    {
        private MainStudentsPanel mainStudentsPanel;

        public MainWorkspace()
        {
            InitializeComponent();
            mainStudentsPanel = new MainStudentsPanel();
            mainStudentsPanel.InputAddressEvent += InputAddressEventHandler;
            mainStudentsPanel.AddNewStudentEvent += AddNewStudenEventHandler;
            mainStudentsPanel.EditStudentEvent += EditStudentEventHandler;
            //mainStudentsPanel.currentStudent.UpdateModelEvent += EditStudentEventHandler;
            ((TabItem)tabControl.Items[0]).Content = mainStudentsPanel;

            var mainTeachersPanel = new MainTeachersPanel();
            ((TabItem)tabControl.Items[1]).Content = mainTeachersPanel;

            var mainSchoolPanel = new MainSchoolsPanel();
            mainSchoolPanel.AddNewSchoolEvent += AddNewSchoolEventHandler;
            ((TabItem)tabControl.Items[2]).Content = mainSchoolPanel;



            var mainSettingsPanel = new MainSettingsPanel();
            ((TabItem)tabControl.Items[4]).Content = mainSettingsPanel;
        }

        protected void InputAddressEventHandler(object sender, EventArgs evArgs)
        {
           // new Window{ Content = new InputAddressPanel { Margin = new Thickness(0), HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center}, Width=300, Height=230, ResizeMode = ResizeMode.NoResize}.ShowDialog();
            var inputAddressPanel = new InputAddressPanel { Margin = new Thickness(0) };
            inputAddressPanel.ApplyInputAddressEvent += delegate(object caller, AddressEventArgs args)
            {
                mainStudentsPanel.currentStudent.Address = args.Address;
                ((Grid)Content).Children.Remove(inputAddressPanel);
            };

            inputAddressPanel.CancelInputAddressEvent += delegate { ((Grid)Content).Children.Remove(inputAddressPanel); };
            ((Grid)Content).Children.Add(inputAddressPanel);
        }


        // добавление новой школы
        protected void AddNewSchoolEventHandler(object sender, EventArgs evArgs)
        {
            var createNewSchoolPanel = new CreateNewSchoolPanel { Margin = new Thickness(0) };

            createNewSchoolPanel.AddNewSchoolEvent += delegate(object caller, Model.ModelEventArgs.SchoolEventArgs args)
            {
                Model.ModelClasses.School newSchool = (Model.ModelClasses.School)args.SchoolArgument;
                DataDispatcher.GetInstance().Schools.Add(newSchool);
                ((Grid)Content).Children.Remove(createNewSchoolPanel);
                ((MainSchoolsPanel)(((TabItem)tabControl.Items[2]).Content)).SchoolsDataGrid.Items.Refresh();
            };

            createNewSchoolPanel.CancelAddNewSchoolEvent += delegate { ((Grid)Content).Children.Remove(createNewSchoolPanel); };
            ((Grid)Content).Children.Add(createNewSchoolPanel);
        }


        // добваление нового студента
        protected void AddNewStudenEventHandler(object sender, Model.ModelEventArgs.StudentEventArgs args)
        {
            var newStudentAddingPanel = new StudentPanel { Margin = new Thickness(0) };
            newStudentAddingPanel.applyChangesButton.Visibility = Visibility.Hidden;
            newStudentAddingPanel.InputAddressEvent += InputAddressEventHandler;
            newStudentAddingPanel.AddNewStudentEvent += delegate(object caller, StudentEventArgs formAddArgs)
            {
                IContactInformation contInf = new Model.ModelClasses.PersonContactInformation();
                contInf.AdditionalPhone = formAddArgs.Student.ContactInformation.AdditionalPhone;
                contInf.Email = formAddArgs.Student.ContactInformation.Email;
                contInf.LandlinePhone = formAddArgs.Student.ContactInformation.LandlinePhone;
                contInf.MobilePhone = formAddArgs.Student.ContactInformation.MobilePhone;
                mainStudentsPanel.currentStudent.ContactInformation = contInf;
                //mainStudentsPanel.currentStudent.
                mainStudentsPanel.currentStudent.Birthday = formAddArgs.Student.Birthday;
                mainStudentsPanel.currentStudent.FatherName = formAddArgs.Student.FatherName;
                mainStudentsPanel.currentStudent.FamilyName = formAddArgs.Student.FamilyName;
                mainStudentsPanel.currentStudent.Name = formAddArgs.Student.Name;

                DataDispatcher.GetInstance().Students.Add(mainStudentsPanel.currentStudent);

                ((Grid)Content).Children.Remove(newStudentAddingPanel);
                ((MainStudentsPanel)(((TabItem)tabControl.Items[0]).Content)).StudentsDataGrid.Items.Refresh();
            };

            newStudentAddingPanel.CancelStudentFormEvent += delegate { ((Grid)Content).Children.Remove(newStudentAddingPanel); };
            ((Grid)Content).Children.Add(newStudentAddingPanel);


            /*Здесь нужно подписаться на ивенты самой формы добавления(применить изменение, сохранить, закрыть, отменить)*/
        }

        protected void EditStudentEventHandler(object sender, Model.ModelEventArgs.StudentEventArgs args)
        {
            var editStudentPanel = new StudentPanel { Margin = new Thickness(0) };
            editStudentPanel.addNewStudentButton.Visibility = Visibility.Hidden;
            editStudentPanel.applyChangesButton.Visibility = Visibility.Visible;
            editStudentPanel.DataContext = (Model.ModelClasses.Student)args.Student;


            editStudentPanel.InputAddressEvent += InputAddressEventHandler;
            editStudentPanel.CancelStudentFormEvent += delegate { ((Grid)Content).Children.Remove(editStudentPanel); };
            editStudentPanel.SubmitStudentChangesEvent += delegate(object caller, StudentEventArgs formEditArgs)
            {
                mainStudentsPanel.currentStudent.ContactInformation.AdditionalPhone = formEditArgs.Student.ContactInformation.AdditionalPhone;
                mainStudentsPanel.currentStudent.ContactInformation.LandlinePhone = formEditArgs.Student.ContactInformation.LandlinePhone;
                mainStudentsPanel.currentStudent.ContactInformation.MobilePhone = formEditArgs.Student.ContactInformation.MobilePhone;
                mainStudentsPanel.currentStudent.Birthday = formEditArgs.Student.Birthday;
                mainStudentsPanel.currentStudent.FatherName = formEditArgs.Student.FatherName;
                mainStudentsPanel.currentStudent.FamilyName = formEditArgs.Student.FamilyName;
                mainStudentsPanel.currentStudent.Name = formEditArgs.Student.Name;
                
                
            };
            ((Grid)Content).Children.Add(editStudentPanel);
        }

        #region TabTextViewing

        private void StudentTabHeaderContentMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)studentTabHeaderContent.Children[1]).Content = "Студенты";
        }

        private void StudentTabHeaderContentMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        { ((Label)studentTabHeaderContent.Children[1]).Content = ""; }

        private void TeachersTabHeaderContentMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)teachersTabHeaderContent.Children[1]).Content = "Преподаватели";
        }

        private void TeachersTabHeaderContentMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)teachersTabHeaderContent.Children[1]).Content = "";
        }

        private void SchoolTabHeaderContentMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)schoolTabHeaderContent.Children[1]).Content = "Школы";
        }

        private void SchoolTabHeaderContentMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)schoolTabHeaderContent.Children[1]).Content = "";
        }

        private void SettingsTabHeaderContentMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)settingsTabHeaderContent.Children[1]).Content = "Настройки";
        }

        private void SettingsTabHeaderContentMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)settingsTabHeaderContent.Children[1]).Content = "";
        }

        private void SubjectsTabHeaderContentMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)subjectsTabHeaderContent.Children[1]).Content = "Предметы";
        }

        private void SubjectsTabHeaderContentMouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            ((Label)subjectsTabHeaderContent.Children[1]).Content = "";
        }
        #endregion
    }
}